using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;

namespace BySoft.Server.DataAccess
{
	public partial class MdProcessDA
	{
		
        public static bool IsProcessExists(string ProcessName, string ProcessID)
        {
            string strcmd = @"SELECT COUNT(ProcessID) FROM " + MdProcessTable.C_TABLENAME + " WHERE 1=1 and ProcessName=@ProcessName  and ProcessID<>@ProcessID";
            DataAccessParameterCollection dpc = new DataAccessParameterCollection();
            dpc.AddWithValue("@ProcessName", ProcessName);
            dpc.AddWithValue("@ProcessID", ProcessID);
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    object obj = broker.ExecuteSQLScalar(strcmd, dpc);
                    if (Convert.ToInt32(obj.ToString().Trim()) > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    throw;
                }
            }
        }

        public static DataTable SelectList(string ProcessName, int pageIndex, int pageSize, out int m_count)
        {
            try
            {
                #region SQL Server
                string strcmd = @"select * from MD_Process where 1=1";

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();

                if (!string.IsNullOrEmpty(ProcessName))
                {
                    strcmd += " and ProcessName LIKE @ProcessName";
                    dpc.AddWithValue("@ProcessName", "%" + ProcessName + "%");
                }

                strcmd += " ORDER BY ProcessID DESC";

                return DataAccessCommon.ExecuteDataTablePaging(strcmd, pageIndex, pageSize, dpc, out m_count);
                #endregion
            }
            catch
            {
                throw;
            }
        }

        public static DataTable SelectListBySchedule(string ProcessName, int pageIndex, int pageSize, out int m_count)
        {
            try
            {
                #region SQL Server
                string strcmd = @"SELECT * FROM MD_Process WHERE 1=1";

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();

                if (!string.IsNullOrEmpty(ProcessName))
                {
                    strcmd += " and ProcessName LIKE @ProcessName";
                    dpc.AddWithValue("@ProcessName", "%" + ProcessName + "%");
                }

                //有效排程中的工序
                strcmd += @" and ProcessID not in (select ProcessID from BS_ScheduleProcess a 
LEFT JOIN BS_ScheduleDetail AS bsd
         ON bsd.ScheduleDetailID = a.ScheduleDetailID
LEFT JOIN BS_Schedule AS bs 
  ON bs.ScheduleID = bsd.ScheduleID   
 where bs.[Status]='1' )";
                strcmd += " ORDER BY ProcessID DESC";

                return DataAccessCommon.ExecuteDataTablePaging(strcmd, pageIndex, pageSize, dpc, out m_count);
                #endregion
            }
            catch
            {
                throw;
            }
        }
    }
}

